﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;



namespace Query
{

    public class GetFromDBService : IGetService
    {
        public List<Rate> Rates = new List<Rate>();

        private List<Rate> ChartDay(string Date, string CurrencyName)
        {
            Rates.Clear();
            if (Date == null)
                return null;
            else
            {
                DateTime DateBegin = Convert.ToDateTime(Date);
                DateTime DateEnd = Convert.ToDateTime(Date).AddDays(1);
                using (ExtRatesEntities context = new ExtRatesEntities())
                {
                    Currency Currency = context.Currency.Where(c => c.Name == CurrencyName).FirstOrDefault();
                    if (Currency != null)
                        foreach (var Rate_Day in context.cache_Day.Where(c => c.CurrencyID == Currency.ID && c.Period >= DateBegin && c.Period <= DateEnd))
                            Rates.Add(new Rate(Rate_Day.ValueMax, Rate_Day.ValueMin, Rate_Day.Value));
                }
                return Rates;
            }
        }
        public List<Rate> ChartDayXML(string Date, string CurrencyName)
        {
            return ChartDay(Date, CurrencyName);
        }
        public List<Rate> ChartDayJson(string Date, string CurrencyName)
        {
            return ChartDay(Date, CurrencyName);
        }

        private List<Rate> ChartWeek(string Date, string CurrencyName)
        {
            Rates.Clear();

            if (Date == null)
                return null;
            else
            {
                DateTime DateBegin = Convert.ToDateTime(Date);
                DateTime DateEnd = Convert.ToDateTime(Date).AddDays(7);
                using (ExtRatesEntities context = new ExtRatesEntities())
                {
                    Currency Currency = context.Currency.Where(c => c.Name == CurrencyName).FirstOrDefault();
                    if (Currency != null)
                        foreach (var Rate_Week in context.cache_Week.Where(c => c.CurrencyID == Currency.ID && c.Period >= DateBegin && c.Period <= DateEnd))
                            Rates.Add(new Rate(Rate_Week.ValueMax, Rate_Week.ValueMin, Rate_Week.Value));
                }
                return Rates;
            }
        }
        public List<Rate> ChartWeekXML(string Date, string CurrencyName)
        {
            return ChartWeek(Date, CurrencyName);
        }
        public List<Rate> ChartWeekJson(string Date, string CurrencyName)
        {
            return ChartWeek(Date, CurrencyName);
        }

        private List<Rate> ChartMonth(string Date, string CurrencyName)
        {
            if (Date == null)
                return null;
            else
            {
                DateTime DateBegin = Convert.ToDateTime(Date).AddDays(1 - Convert.ToDateTime(Date).Day);
                DateTime DateEnd = DateBegin.AddMonths(1);

                using (ExtRatesEntities context = new ExtRatesEntities())
                {
                    Currency Currency = context.Currency.Where(c => c.Name == CurrencyName).FirstOrDefault();
                    if (Currency != null)
                        foreach (var Rate_Month in context.cache_Month.Where(c => c.CurrencyID == Currency.ID && c.Period >= DateBegin && c.Period <= DateEnd))
                            Rates.Add(new Rate(Rate_Month.ValueMax, Rate_Month.ValueMin, Rate_Month.Value));
                }
                return Rates;
            }
        }
        public List<Rate> ChartMonthXML(string Date, string CurrencyName)
        {
            return ChartMonth(Date, CurrencyName);
        }
        public List<Rate> ChartMonthJson(string Date, string CurrencyName)
        {
            return ChartMonth(Date, CurrencyName);
        }

        private List<Rate> ChartYear(string Date, string CurrencyName)
        {
            Rates.Clear();

            if (Date == null)
                return null;
            else
            {
                DateTime DateBegin = Convert.ToDateTime(Date).AddDays(1 - Convert.ToDateTime(Date).Day).AddMonths(1 - Convert.ToDateTime(Date).Month);
                DateTime DateEnd = DateBegin.AddYears(1);
                using (ExtRatesEntities context = new ExtRatesEntities())
                {
                    Currency Currency = context.Currency.Where(c => c.Name == CurrencyName).FirstOrDefault();
                    if (Currency != null)
                        foreach (var Rate_Year in context.cache_Year.Where(c => c.CurrencyID == Currency.ID && c.Period >= DateBegin && c.Period <= DateEnd))
                            Rates.Add(new Rate(Rate_Year.ValueMax, Rate_Year.ValueMin, Rate_Year.Value));
                }
                return Rates;
            }
        }
        public List<Rate> ChartYearXML(string Date, string CurrencyName)
        {
            return ChartYear(Date, CurrencyName);
        }
        public List<Rate> ChartYearJson(string Date, string CurrencyName)
        {
            return ChartYear(Date, CurrencyName);
        }

    }

}
